from django.shortcuts import render, redirect
# 导包
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
# 导入类视图
from django.views import View
import jwt
from myapp.models import User, Pics
import json

from rest_framework.response import Response
from rest_framework.views import APIView
# 导入加密库
import hashlib
# 导入图片库
# 绘画库
from PIL import ImageDraw
# 字体库
from PIL import ImageFont
# 图片库
from PIL import Image
# 随机库
import random
# 文件流
import io

import requests

# 导入上传文件夹配置
from mydjango.settings import UPLOAD_ROOT
import os

# 导入原生sql模块
from django.db import connection

import jwt

# 导入redis数据库
import redis

# 导入时间模块
import time

# 导入公共目录变量
from mydjango.settings import BASE_DIR

# 导包
from django.db.models import Q, F

# 导入dwebsocket的库
from dwebsocket.decorators import accept_websocket
import uuid
from myapp.myser import UserSer, PicsSer
import redis
from django.utils.deprecation import MiddlewareMixin

# 定义地址和端口
host = '127.0.0.1'
port = 6379
# 建立redis连接
r = redis.Redis(host=host, port=port)


# 幻灯片接口
class PicsInfo(APIView):
    def get(self, request):
        # 读取数据库
        pics = Pics.objects.filter().values("title","link","img")
        resp = {}
        resp['code'] = 200
        resp['data'] = PicsSer(pics, many=True).data
        # 返回
        return Response(resp)


# 插入幻灯片
class InsertPics(APIView):
    def get(self, request):
        # 读取数据库
        pics = Pics.objects.all()
        resp = {}
        resp['code'] = 200
        resp['data'] = PicsSer(pics, many=True).data
        # 返回
        return Response(resp)

    def post(self, request):
        # 接收参数
        title = request.POST.get('title', 'null')
        link = request.POST.get('link', 'null')
        img = request.FILES.get('img', 'null')
        # 将获取到的图片添加到upload文件中
        with open(os.path.join(UPLOAD_ROOT, "", img.name), 'wb') as f:
            for chunk in img.chunks():
                f.write(chunk)
        # 排重操作
        pices = Pics.objects.filter(title=title).first()
        if pices:
            res = {}
            res['code'] = 405
            res['message'] = '该活动已经存在'
            return Response(res)

        # 进行入库操作
        pics = Pics(title=title, link=link, img='http://localhost:8000/static/upload/'+str(img))
        # 保存
        pics.save()

        # 返回结果
        res = {}
        res['code'] = 200
        res['message'] = '添加成功'
        return Response(res)

    def delete(self, request):
        # 接收参数
        id = request.data.get('id')
        pics = Pics.objects.filter(id=id).first()
        pics.delete()
        res = {}
        res['code'] = 200
        res['msg'] = '删除成功'
        return Response(res)

    def put(self, request):
        # 接收参数
        cid = request.data.get("id")
        title = request.data.get('title', 'null')
        link = request.data.get('link', 'null')
        img = request.data.get('img', 'null')
        # 将获取到的图片添加到upload文件中
        with open(os.path.join(UPLOAD_ROOT, "", img.name), 'wb') as f:
            for chunk in img.chunks():
                f.write(chunk)
        # 查找数据
        pices = Pics.objects.filter(id=cid).first()
        pices.title = title
        pices.link = link
        pices.img = 'http://localhost:8000/static/upload/'+str(img)
        pices.save()
        res = {}
        res['code'] = 200
        res['message'] = '修改成功'
        return Response(res)


